//
//  ZFK_scrollPicView.m
//  Drops
//
//  Created by SHG on 16/5/20.
//  Copyright © 2016年 Drops.er. All rights reserved.
//

#import "ZFK_scrollPicView.h"
#import "ZFK_scrollPic_CollectionViewCell.h"
#import "UIKit+AFNetworking.h"
#import "ZFK_header.h"

@implementation ZFK_scrollPicView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
    }
    return self;
}

- (void)createView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(sWIDTH, sHEIGHT / 2 - sHEIGHT / 7);
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.mainCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, sWIDTH, sHEIGHT / 2 - sHEIGHT / 7) collectionViewLayout:layout];
    self.mainCollectionView.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:self.mainCollectionView];
    
    self.mainCollectionView.delegate = self;
    self.mainCollectionView.dataSource = self;
    
    [self.mainCollectionView registerClass:[ZFK_scrollPic_CollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([ZFK_scrollPic_CollectionViewCell class])];
    
    self.mainCollectionView.contentOffset = CGPointMake(sWIDTH, 0);
    self.mainCollectionView.pagingEnabled = YES;
    self.mainCollectionView.showsHorizontalScrollIndicator = NO;
    
    [self addSubview:self.mainCollectionView];
    
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, HEIGHT - 30, WIDTH, 30)];
    self.page.hidesForSinglePage = YES;
    [self addSubview:self.page];
    
    self.page.backgroundColor = [UIColor clearColor];
    self.page.currentPageIndicatorTintColor = [UIColor whiteColor];
    self.page.pageIndicatorTintColor = [UIColor lightGrayColor];
    
    [self.page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];

    self.page.numberOfPages = self.scrollPicArray.count - 2;
    
    self.page.currentPage = self.mainCollectionView.contentOffset.x / sWIDTH - 1;
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSLog(@"counttttt:%ld", self.scrollPicArray.count);
    return self.scrollPicArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ZFK_scrollPic_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([ZFK_scrollPic_CollectionViewCell class]) forIndexPath:indexPath];
    ZFK_header *header = [[ZFK_header alloc] init];
    [header setValuesForKeysWithDictionary:self.scrollPicArray[indexPath.row]];
    [cell.imageView setImageWithURL:[NSURL URLWithString:header.activityImage]];
    cell.titleLabel.text = header.title;
    self.page.numberOfPages = self.scrollPicArray.count - 2;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.mainCollectionView.contentOffset.x == 0) {
        self.mainCollectionView.contentOffset = CGPointMake((self.scrollPicArray.count - 2) * sWIDTH, 0);
    } else if (self.mainCollectionView.contentOffset.x == (self.scrollPicArray.count - 1) * sWIDTH) {
        self.mainCollectionView.contentOffset = CGPointMake(sWIDTH, 0);
    }
    self.page.currentPage = self.mainCollectionView.contentOffset.x / sWIDTH - 1;
}

- (void)pageAction:(UIPageControl *)page {
    self.mainCollectionView.contentOffset = CGPointMake(sWIDTH * (page.currentPage + 1), 0);
    
    [self.mainCollectionView setContentOffset:CGPointMake((sWIDTH * (page.currentPage + 1)), 0) animated:YES];
}

@end
